package com.you.concurrent.blockingqueue;

import java.util.concurrent.LinkedBlockingQueue;

public class LinkedBlockingQueueDemo {

    static LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue();

    public static void main(String[] args) {

        for (int i = 0; i < 3; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(1000L);
                            //linkedBlockingQueue.offer(index);
                            linkedBlockingQueue.put(index);
                            System.out.println(Thread.currentThread().getName() + " add num is : " + index);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }

        for (int i = 0; i < 20; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(10L);
                            Integer value = linkedBlockingQueue.take();
                            System.out.println("----------" + Thread.currentThread().getName() + " get num is : " + value);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }
}
